/*
 *
 *  This file is part of Cfu
 *
 *  Cfu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Cfu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Cfu. If not, see <http://www.gnu.org/licenses/>.
 *
 *  submitter.cc - william le ferrand (william@corefarm.com) 29/04/2010 15:30
 *
 */
 

// STL
#include <stdio.h>
#include <iostream>
#include <sstream>

// QT
#include <QMenu>
#include <QMessageBox>
#include <QDebug>
#include <QApplication>

// BOOST 
#include <boost/filesystem.hpp>

// CFU
#include <errors.hpp>
#include <submitter.hpp>
#include <parser.hpp>
#include <connector.hpp>
#include <mainwindow.hpp>

submitter_t::submitter_t (configuration_t *conf) : _conf (conf)
{
  std::cout << "@@@ Building submitter\n";   
}

void submitter_t::submit (QString &file, int ghz, int is_exr) 
{
  std::cout << "@@@ In the submitter\n"; 
  QString Qlogin = _conf->getLogin (); 
  QString Qpassword = _conf->getPassword (); 
  QString Qserver = _conf->getServer (); 

  std::string login = Qlogin.toStdString (); 
  std::string password = Qpassword.toStdString (); 
  std::string server = Qserver.toStdString (); 
  std::string filename = file.toStdString (); 
  std::cout << "@@@ Check_filenames\n" ; 
  _mainwindow->updateStatus (QString ("Processing sanity checks on the XML file")); 

  sanity (file); 
 
  std::cout << "@@@ Opening the job on the corefarm\n"; 
  _mainwindow->updateStatus (QString ("Opening job on Corefarm")); 
  std::string job_id = new_job (server, login, password) ; 
  std::cout << "@@@ Uploading textures\n"; 
  _mainwindow->updateStatus (QString ("Uploading textures")); 
  struct checker *chk = (struct checker *) malloc ((sizeof (struct checker))); 

  chk->status = 1; 
  chk->msg = 0; 
  chk->upload = true; 
  chk->job_id = &job_id ;
  chk->server = &server ; 
  chk->login = &login ; 
  chk->password = &password ;
  chk->mainWindow = _mainwindow; 
  
  std::cout << "@@@ Into the parser\n"; 

  check_filenames ((filename).c_str (), chk); 
   
  free (chk); 

  std::cout << "@@@ Uploading XML file\n"; 
  _mainwindow->updateStatus (QString ("Uploading XML file")); 
  
  drop_compressed_file (server, login, password, job_id, filename); 
 
  std::cout << "@@@ Triggering the job start\n"; 
  
  boost::filesystem::path f = boost::filesystem::path (filename) ; 
  std::string basename = f.filename () ; // This is the targetted name 
  
  std::ostringstream input; 
  input << "$" << basename << ".gz$ " << is_exr; 
  std::string input_s = input.str () ;

  start_job (server, login, password, job_id, ghz, input_s); 

  return ; 

}

void submitter_t::sanity (QString &file)
{
   struct checker *chk = (struct checker *) malloc ((sizeof (struct checker))); 
   chk->status = 1; 
   chk->msg = 0; 
   chk->upload = false; 
   
   check_filenames ((file.toStdString ()).c_str (), chk); 
   
   std::cout << "@@@ Status after parsing: " << chk->status << std::endl; 
   if (!chk->status) 
     {
       std::string file (chk->msg);
       free (chk); 
       throw (missing_file (file)); 
     }
  
   free (chk); 
   return; 
}

